package com.wondersgroup.framework.widget.taglib;

import com.wondersgroup.framework.widget.annotations.WidgetTag;
import com.wondersgroup.framework.widget.annotations.WidgetTagAttribute;
import com.wondersgroup.framework.widget.context.DefaultSettings;
import com.wondersgroup.framework.widget.util.OutputUtils;
import com.wondersgroup.framework.widget.util.TagUtils;
import com.wondersgroup.framework.widget.validation.annotation.TypeValidate;
import com.wondersgroup.wssip.application.SessionConstants;
import java.util.HashSet;
import javax.servlet.http.Cookie;
import javax.servlet.jsp.JspException;
import org.apache.struts2.ServletActionContext;

@WidgetTag(name="base", tldTagClass="com.wondersgroup.framework.widget.taglib.BaseTag", description="")
public class BaseTag extends WidgetTagSupport {
  
	/**
	 * 添加固定版本号
	 */
	private static final long serialVersionUID = 1L;
	@TypeValidate
	private boolean subPage = false;
	private String namespace;
	
	public String getNamespace() {
		return this.namespace;
	}

	@WidgetTagAttribute(description="命名空间，当为子页面时必须设置")
	public void setNamespace(String paramString) {
		this.namespace = paramString;
	}

	public boolean isSubPage() {
		return this.subPage;
	}

	@WidgetTagAttribute(description="是否为子页面", defaultValue="false", type="Boolean")
	public void setSubPage(boolean paramBoolean) {
		this.subPage = paramBoolean;
	}

	@SuppressWarnings("rawtypes")
	public void processTagContent() throws JspException {
		String skinName = "default";
		Cookie[] cookie = ServletActionContext.getRequest().getCookies();
		if(cookie!=null){
			for (int i = 0; i < cookie.length; i++) {
				Cookie cook = cookie[i];
				if (cook.getName().equalsIgnoreCase(SessionConstants.WSSIP_CLIENT_SKINNAME)) {
					skinName = cook.getValue();
				}
			}
		}
		
		if (this.pageContext.getRequest().getAttribute("widget.scripts.loaded") == null){
			this.pageContext.getRequest().setAttribute("widget.scripts.loaded", new HashSet());
		}
    
	    if (!(this.subPage)) {
	    	StringBuffer localStringBuffer = new StringBuffer();
	    	String str = DefaultSettings.get("widget.theme");
	    	localStringBuffer.append(OutputUtils.writeStyleSheet(this.pageContext, new String[] { "resources-"+skinName+"/css/ext-all.css", "resources-"+skinName+"/css/wonders.css" }));
	    	if (!(str.equals("default"))){
	    		localStringBuffer.append(OutputUtils.writeStyleSheet(this.pageContext, new String[] { "resources-"+skinName+"/css/xtheme-" + str + ".css" }));
	    	}
	    	localStringBuffer.append(OutputUtils.writeJSFile(this.pageContext, new String[] { "extjs/base/ext-core.js", "extjs/base/widget-core.js" }));
	    	localStringBuffer.append(OutputUtils.writeJSFile(this.pageContext, new String[] { "wonders/wonders-base-min.js" }));
	    	localStringBuffer.append(OutputUtils.writeJSFile(this.pageContext, new String[] { "wonders/cookieUtils.js" }));
	    	localStringBuffer.append("<script>");
	    	localStringBuffer.append("cf_r(function(){");
	    	localStringBuffer.append("Ext.BLANK_IMAGE_URL=\"" + TagUtils.getLibraryPath(this.pageContext) + "resources-"+skinName+"/images/default/s.gif\";");
	    	localStringBuffer.append("Ext.QuickTips.init();");
	    	localStringBuffer.append("if(Ext.data.Connection){");
	    	localStringBuffer.append("    Ext.apply(Ext.data.Connection.prototype, {");
	    	localStringBuffer.append("       timeout: " + (DefaultSettings.getInt("widget.backovertime") * 1000));
	    	localStringBuffer.append("    });");
	    	localStringBuffer.append("}");
	    	localStringBuffer.append("if(Ext.form.BasicForm){");
	    	localStringBuffer.append("    Ext.apply(Ext.form.BasicForm.prototype, {");
	    	localStringBuffer.append("       timeout: " + DefaultSettings.getInt("widget.backovertime"));
	    	localStringBuffer.append("    });");
	    	localStringBuffer.append("}");
	    	localStringBuffer.append("});");
	    	localStringBuffer.append("</script>");
	    	TagUtils.getInstance().write(this.pageContext, localStringBuffer.toString());
	    }
	    
		if (this.namespace == null){
			this.namespace = "cw";
		}
		
		this.pageContext.setAttribute("page.namespace", this.namespace);
	}
}
